package com.github.shuaidd.rest.runtime;

import com.github.shuaidd.service.runtime.LwfActivitiTaskFormService;
import lombok.RequiredArgsConstructor;
import org.activiti.app.model.runtime.CompleteFormRepresentation;
import org.activiti.app.model.runtime.ProcessInstanceVariableRepresentation;
import org.activiti.form.model.FormDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("lwfTaskFormResource")
@RequestMapping("/app/rest/task-forms")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class TaskFormResource {

    private final LwfActivitiTaskFormService taskFormService;

    @RequestMapping(value = "/{taskId}", method = RequestMethod.GET, produces = "application/json")
    public FormDefinition getTaskForm(@PathVariable String taskId) {
        return taskFormService.getTaskForm(taskId);
    }

    @ResponseStatus(value = HttpStatus.OK)
    @RequestMapping(value = "/{taskId}", method = RequestMethod.POST, produces = "application/json")
    public void completeTaskForm(@PathVariable String taskId, @RequestBody CompleteFormRepresentation completeTaskFormRepresentation) {
        taskFormService.completeTaskForm(taskId, completeTaskFormRepresentation);
    }

    @RequestMapping(value = "/{taskId}/variables", method = RequestMethod.GET, produces = "application/json")
    public List<ProcessInstanceVariableRepresentation> getProcessInstanceVariables(@PathVariable String taskId) {
        return taskFormService.getProcessInstanceVariables(taskId);
    }
}
